package cn.etynit.web.controller;

import cn.etynit.service.IExpenseService;
import cn.etynit.domain.Expense;
import cn.etynit.query.ExpenseQuery;
import cn.etynit.result.JsonResult;
import cn.etynit.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/expense")
@Api(value = "业主月账单接口",description = "此接口是业主月账单相关的类")
public class ExpenseController {

    @Autowired
    public IExpenseService expenseService;

    /**
    * 保存和修改公用的
    */
    @ApiOperation(value = "保存和修改业主费用账单",notes = "保存和修改业主费用账单",response = JsonResult.class)
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody Expense expense){
        if(expense.getId()!=null){
            expenseService.updateById(expense);
        }else{
            expenseService.insert(expense);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @ApiOperation(value = "根据id删除业主费用账单",notes = "根据id删除业主费用账单",response = JsonResult.class)
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        expenseService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @ApiOperation(value = "根据id查询业主费用账单",notes = "根据id查询业主费用账单",response = JsonResult.class)
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(expenseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @ApiOperation(value = "查询所有业主费用账单",notes = "查询所有业主费用账单",response = JsonResult.class)
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(expenseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @ApiOperation(value = "分页查询所有业主费用账单",notes = "分页查询所有业主费用账单",response = JsonResult.class)
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody ExpenseQuery query){
        Page<Expense> page = new Page<Expense>(query.getPage(),query.getRows());
        page = expenseService.selectPage(page);
        return JsonResult.success(new PageList<Expense>(page.getTotal(),page.getRecords()));
    }

    /**
     * 根据type类型查账单
     * @param type
     * @return
     */
    @ApiOperation(value = "查询业主月账单",notes = "查询业主月账单",response = JsonResult.class)
    @RequestMapping(value = "/inquireCost/{type}/{userId}/{month}",method = RequestMethod.GET)
    public JsonResult inquireCost(@PathVariable("type") Integer type,@PathVariable("userId") Long userId,@PathVariable("month") Integer month){
        return JsonResult.success(expenseService.inquireCost(type,userId,month));
    }

    /**
     * 费用自动推送
     * @return
     */
    @RequestMapping(value = "/adviceCharge",method = RequestMethod.GET)
    public JsonResult adviceCharge(){
        return JsonResult.success(expenseService.adviceCharge());
    }
}
